package com.bytenebula;

import com.bytenebula.asteroid.entity.TenantEntity;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.zaxxer.hikari.HikariDataSource;

public class Codegen {
    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:postgresql://127.0.0.1:5432/bytenebula?characterEncoding=utf-8");
        dataSource.setUsername("postgres");
        dataSource.setPassword("pgp@ssw0rd");


        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.getPackageConfig()
                .setSourceDir("./build/generated-sources")
                .setBasePackage("com.bytenebula.asteroid");

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setIgnoreColumns("id", "is_delete", "is_active", "create_time", "create_user_id", "last_modified_time", "last_modified_user_id", "tenant_id");


        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setSuperClass(TenantEntity.class)
                .setWithLombok(true)
                .setJdkVersion(17);

        globalConfig.getMapperXmlConfig()
                .setFileSuffix("Mapper");

        //可以单独配置某个列
        //tenant_id
//        ColumnConfig tenantIdColumnConfig = new ColumnConfig();
//        tenantIdColumnConfig.setColumnName("tenant_id");
//        tenantIdColumnConfig.setTenantId(true);
//        globalConfig.setColumnConfig(tenantIdColumnConfig);

//        id
//        ColumnConfig idColumnConfig = new ColumnConfig();
//        idColumnConfig.setColumnName("id");
//        idColumnConfig.setPrimaryKey(true);
//        globalConfig.setColumnConfig(idColumnConfig);

        //state
//        ColumnConfig stateColumnConfig = new ColumnConfig();
//        stateColumnConfig.setColumnName("state");
//        stateColumnConfig.setLogicDelete(true);
//        globalConfig.setColumnConfig(stateColumnConfig);

        //设置生成内容
//        globalConfig.disableEntity();
        globalConfig.enableMapper();
        globalConfig.enableController();
        globalConfig.enableService();
        globalConfig.enableServiceImpl();
        globalConfig.enableMapperXml();
        globalConfig.enableTableDef();

        //生成代码
        Generator generator = new Generator(dataSource, globalConfig);
        generator.generate();


    }
}
